<template>
    <div>
        <el-dialog :visible.sync="value" :show-close="false" center>
            <el-cascader-panel v-model="selected" :options="departmentList" :props="{multiple:true,emitPath:false,value:'id',label:'label',children:'children'}"></el-cascader-panel>
            <template slot="footer">
                <Button type="primary" icon="md-add" v-loading.fullscreen.lock="fullscreenLoading" @click="handleSubmit">提交</Button>
            </template>
        </el-dialog>
    </div>
</template>

<script>

    import { searchDepartmentBySchool } from '@/api/principal/school-manager/school'
    import { saveOrUpdateTeacherDepartment } from '@/api/principal/school-manager/department'
    export default {
        name: "school-teacher-department",
        data(){
            return {
                value : false,
                parent:{},
                departmentList: [],
                selected:[],
                fullscreenLoading:false
            }
        },
        watch:{
            value(data){
                if(data){
                    searchDepartmentBySchool().then(res=>{
                        this.departmentList = res.data.data;
                    });

                    setTimeout(()=>{
                        if(this.parent.departmentIds){
                            this.selected = this.parent.departmentIds.split(",");
                        }

                    },500)
                }
            }
        },
        methods:{
            handleSubmit(){
                if(this.selected.length == 0){
                    this.$message.error({message:'请选择部门',center:true});
                    return false;
                }

                this.$confirm("确定要提交吗？","温馨提示",{type:'warning'}).then(()=>{
                    this.fullscreenLoading = true;

                    let departmentIds = this.selected.join(",");
                    let userId = this.parent.id;

                    saveOrUpdateTeacherDepartment({userId:userId,departmentIds:departmentIds}).then(res=>{
                        this.value = false;
                        this.fullscreenLoading = false;
                        this.$parent.getTeacherList(this.$parent.params);
                    })
                })

            }
        }
    }
</script>

<style scoped>

</style>